//
//  PostImagesView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/5.
//

import SwiftUI
import URLImage

struct  PostImagesView: View {
    let images: [String]
    static let spacing: CGFloat = 5
    static let maxWidth = Bounds.width - 64
    
    var body: some View {
        Group {
            switch images.count {
            case 1:
                SingleImageView(images: images)
                    
            case 2:
                TwoImagesView(images: images)
            case 3:
                ThreeImagesView(images: images)
            case 4:
                FourImagesView(images: images)
            default:
                GridImagesView(images: images)
            }
        }
    }
    
    // 单图展示（4:3比例）
    struct SingleImageView: View {
        @State private var showPreview = false
        @State private var selectedIndex = 0
        let images: [String]
        var body: some View {
            ForEach(images, id: \.self) { url in
                if let imageUrl = URL(string: url) {
                    AsyncImage(url: imageUrl) { image in
                        image.resizable()
                            .aspectRatio(contentMode: .fill)
//                            .frame(width: maxWidth, height: maxWidth * 0.75)
                            .frame(width: (maxWidth - spacing)/2, height: (maxWidth - spacing)/2)

                            .clipped()
                            .cornerRadius(8)
                            .onTapGesture {
                                selectedIndex = 0
                                showPreview = true
                            }
                            .fullScreenCover(isPresented: $showPreview) {
                                PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                            }
                    } placeholder: {
                        ProgressView()
                    }
                }
            }
        }
    }
    
    // 两图展示（1:1比例）
    struct TwoImagesView: View {
        let images: [String]
        @State private var showPreview = false
        @State private var selectedIndex = 0
        var body: some View {
            HStack(spacing: spacing) {
                ForEach(images, id: \.self) { url in
                    if let imageUrl = URL(string: url) {
                        AsyncImage(url: imageUrl) { image in
                            image.resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: (maxWidth - spacing)/2, height: (maxWidth - spacing)/2)
                                .clipped()
                                .cornerRadius(4)
                                .onTapGesture {
                                    selectedIndex = images.firstIndex(of: url) ?? 0
                                    showPreview = true
                                }
                                .fullScreenCover(isPresented: $showPreview) {
                                    PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                                }
                            
                        } placeholder: {
                            ProgressView()
                        }
                    }
                }
            }
        }
    }
    
    // 三图展示（右大图）
    struct ThreeImagesView: View {
        let images: [String]
        @State private var showPreview = false
        @State private var selectedIndex = 0
        var body: some View {
            HStack(spacing: spacing) {
                if images.count >= 1, let url1 = URL(string: images[0]) {
                    AsyncImage(url: url1) { image in
                        image.resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: (maxWidth - spacing*2)/3, height: (maxWidth - spacing*2)/3 * 2)
                            .clipped()
                            .cornerRadius(4)
                            .onTapGesture {
                                selectedIndex =  0
                                showPreview = true
                            }
                            .fullScreenCover(isPresented: $showPreview) {
                                PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                            }
                    } placeholder: {
                        ProgressView()
                    }
                }
                
                if images.count >= 2 {
                    VStack(spacing: spacing) {
                        ForEach(1..<min(3, images.count), id: \.self) { index in
                            if let url = URL(string: images[index]) {
                                AsyncImage(url: url) { image in
                                    image.resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .frame(width: (maxWidth - spacing*2)/3 * 2, height: (maxWidth - spacing*2)/3)
                                        .clipped()
                                        .cornerRadius(4)
                                        .onTapGesture {
                                            selectedIndex =  index
                                            showPreview = true
                                        }
                                        .fullScreenCover(isPresented: $showPreview) {
                                            PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                                        }
                                } placeholder: {
                                    ProgressView()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    // 四图展示（2x2网格）
    struct FourImagesView: View {
        let images: [String]
        @State private var showPreview = false
        @State private var selectedIndex = 0
        var body: some View {
            VStack(spacing: spacing) {
                HStack(spacing: spacing) {
                    ForEach(0..<2, id: \.self) { index in
                        if let url = URL(string: images[index]) {
                            AsyncImage(url: url) { image in
                                image.resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: (maxWidth - spacing)/2, height: (maxWidth - spacing)/2)
                                    .clipped()
                                    .cornerRadius(4)
                                    .onTapGesture {
                                        selectedIndex =  index
                                        showPreview = true
                                    }
                                    .fullScreenCover(isPresented: $showPreview) {
                                        PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                                    }
                            } placeholder: {
                                ProgressView()
                            }
                        }
                    }
                }
                
                HStack(spacing: spacing) {
                    ForEach(2..<4, id: \.self) { index in
                        if let url = URL(string: images[index]) {
                            AsyncImage(url: url) { image in
                                image.resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: (maxWidth - spacing)/2, height: (maxWidth - spacing)/2)
                                    .clipped()
                                    .cornerRadius(4)
                                    .onTapGesture {
                                        selectedIndex =  index
                                        showPreview = true
                                    }
                                    .fullScreenCover(isPresented: $showPreview) {
                                        PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                                    }
                            } placeholder: {
                                ProgressView()
                            }
                        }
                    }
                }
            }
        }
    }
    
    // 多图展示（3x3网格）
    struct GridImagesView: View {
        let images: [String]
        @State private var showPreview = false
        @State private var selectedIndex = 0
        var body: some View {
            let columns = [
                GridItem(.flexible(), spacing: spacing),
                GridItem(.flexible(), spacing: spacing),
                GridItem(.flexible(), spacing: spacing)
            ]
            
            LazyVGrid(columns: columns, spacing: spacing) {
                ForEach(0..<min(9, images.count), id: \.self) { index in
                    if let url = URL(string: images[index]) {
                        AsyncImage(url: url) { image in
                            image.resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: (maxWidth - spacing*2)/3, height: (maxWidth - spacing*2)/3)
                                .clipped()
                                .cornerRadius(4)
                                .onTapGesture {
                                    selectedIndex =  index
                                    showPreview = true
                                }
                                .fullScreenCover(isPresented: $showPreview) {
                                    PhotoPreviewView(images: images, currentIndex: $selectedIndex)
                                }
                        } placeholder: {
                            ProgressView()
                        }
                    }
                }
            }
        }
    }
}
